#define _CRT_SECURE_NO_WARNINGS 1
#include "SeqList.h"

void SeqInit(SL* ps){
	ps->size = 0;
	ps->a = (SLDateType*)malloc(4 * sizeof(SLDateType));
	if (ps->a == NULL)
	{
		perror("malloc fail");
		return;
	}
	ps->capacity = 4;
}

void SeqDestroy(SL* ps) {
	free(ps->a);
	ps->a = NULL;
	ps->size = 0;
	ps->capacity = 0;
}

void SeqPrint(SL* ps) {
	for (int i = 0; i < ps->size; i++)
		printf("%d ", ps->a[i]);
	printf("\n");
}

void SeqCheck(SL* ps) {
	if (ps->size == ps->capacity) {
		SLDateType* tmp = (SLDateType*)realloc(ps->a, 2 * ps->capacity * sizeof(SLDateType));
		ps->a = tmp;
		ps->capacity *= 2;
	}

	
}

void Backpush(SL* ps,SLDateType x) {
	SeqCheck(ps);
	ps->a[ps->size++] = x;
}

void Backpop(SL* ps) {
	assert(ps->size > 0);
	ps->a[ps->size - 1] = 0;
	ps->size--;
}

int Find(SL* ps,SLDateType x) {
	assert(ps);
	for (int i = 0; i < ps->size; i++)
	{
		if (ps->a[i] == x)
			return i;
	}
	return -1;
}

void SeqInsert(SL* ps, int dst, SLDateType x) {
	SeqCheck(ps);
	assert(dst >= 0 && dst <= ps->size);
	int end = ps->size++;
	while (end>dst ) {
		ps->a[end] = ps->a[end - 1];
		end--;
	}
	ps->a[dst] = x;
}

void SeqErase(SL* ps, int pos) {
	SeqCheck(ps);
	assert(pos >= 0 && pos < ps->size);
	int begin = pos;
	while (begin < ps->size-1) {
		ps->a[begin] = ps->a[begin + 1];
		begin++;
	}
	ps->size--;
}

